package queue;

public class MyLinkQueue {
        static class ListNode {
            public int val;
            public ListNode prev;
            public ListNode next;

            public ListNode(int val) {
                this.val = val;
            }
        }

        public ListNode head;
        public ListNode tail;
        public int usedSize;
        public boolean offer(int val) {
            ListNode node = new ListNode(val);
            if(head==null) {
                head = tail = node;
            } else {
                tail.next = node;
                node.prev = tail;
                tail = tail.next;
            }
            usedSize++;
            return true;
        }

        public int poll() {
            if(head==null) {
                throw new QueueNullException("队列为空");
            }
            int val = head.val;
            if(head.next==null) {
                head = null;
                tail = null;
                return val;
            }
            head = head.next;
            head.prev = null;
            usedSize--;
            return val;
        }
        public int peek() {
            if(head==null) {
                throw new QueueNullException("队列为空");
            }
            return head.val;
        }
        public boolean empty() {
            return head==null;
        }
        public int size() {
            return usedSize;
        }
}
